package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.sxfq.loansupermarket.backsystem.common.constant.factory.ConstantFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.UserMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.core.log.LogObjectHolder;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 进入考试入口
 *
 * @author py
 * @Date 2018年2月4日23:05:54
 */
@Controller
@RequestMapping("/testMenu")
public class TestMenuController extends BaseController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    private SqlMapper sqlMapper;

    @RequestMapping("")
    public String to_my(Model model) {
        Integer userId = ShiroKit.getUser().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        User user = this.userMapper.selectById(userId);
        if (user.getAvatar() == null || user.getAvatar() == "") {
            user.setAvatar("http://yiyayl.com/photo/logo.png");
        }
        model.addAttribute(user);
        model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleid()));
        model.addAttribute("deptName", ConstantFactory.me().getDeptName(user.getDeptid()));
        LogObjectHolder.me().set(user);
        model.addAttribute("userName", user.getName());
        model.addAttribute("avatar", user.getAvatar());
        model.addAttribute("testScores",ShiroKit.getSessionAttr("testScores"));
        return "/theTest/btms.html";
    }

    @RequestMapping("/delete")
    public String to_delete(Model model) {
        Integer userId = ShiroKit.getUser().getId();
        String sql = "delete from error_questions_info where user_id=" + userId + " ";
        sqlMapper.delete(sql);
        return "/theTest/btms.html";
    }
}
